/**
 * Created by sunshine on 17-5-3.
 */
$(document).ready(function () {
    $('#frame').html($('#index_Tpl').html());

});
//type = 1 为展示页面，否则为添加页面
function getTpl(index, type) {
    showHightLight(index);
    var frame = $('#frame');
    switch (index) {
        case 1:
            frame.html($('#index_Tpl').text());
            break;
        case 2:
            frame.html($('#findStudentInfo_Tpl').text());
            break;
        case 3:
            frame.html($('#insertStudentInfo_Tpl').html());
            $('#addInfoButton').click(function () {
                addStudentInfo();
            });
            getCollege(type);
            $('#college').bind('change',function () {
                getSpecialty();
            });
            break;
        case 10:

    }
}

function addStudentInfo() {
    if (!ifEmpty()) {
        $.ajax({
            url: '../Student/ajaxaddstudent',
            type: 'POST',
            data: $('#insertForm').serialize(),
            dataType: 'json',
            success: function (result) {
                if (result.status == true) {
                    alert('添加成功');
                } else {
                    alert('添加失败');
                }
            },
            error: function (status) {
                alert('服务器故障');
            }
        })
    }

}

function showHightLight(n) {
    $('#listGroup').children().attr('class', 'list-group-item');
    $('#list' + n).addClass('active');
}

function showStudent(sid, type) {
    $.ajax({
        url: '../Student/ajaxgetstudent',
        type: 'POST',
        data: {sid: sid},
        dataType: 'json',
        success: function (result) {
            //加载模板，只加载学院，不加载学院专业，等待下面获取学院后加载专业
            getTpl(3, 1);

            $('#studentname').val(result.info.stuname);
            $('#studentid').val(result.info.sid);
            $('#borndate').val(result.info.stuborn);
            if (result.info.stusex == 'boy') {
                $('#sex1').attr('checked', '');
            } else {
                $('#sex2').attr('checked', '');
            }
            $('#college option[value = ' + result.info.stucollege + ']').attr('selected', true);
            //这里获取了学院信息，传递需要选择的专业和专业sid
            getSpecialty(1, result.info.stuspecialty);
            $('#specialty option[value = ' + result.info.stuspecialty + ']').attr('selected', true);
            $('#studenclass').val(result.info.stuclass);
            $('#politics option[value = ' + result.info.stupolitics + ']').attr('selected', true);
            $('#minority option[value = ' + result.info.stuminority + ']').attr('selected', true);
            $('#studenttel').val(result.info.stutel);
            $('#studentemail').val(result.info.stuemail);
            $('#studentlocation').val(result.info.stulocation);

            $('#insertForm *').each(function () {
                $(this).attr('readonly', 'true');
            });
            $('#addInfoButton').after('<a class="btn btn-primary" id="changeEnableButton" onclick="changeenable()">编辑</a>');
            $('#addInfoButton').remove();
            //如果是编辑调用
            if (type) {
                changeenable();
            }

        },
        error: function (error) {

        }
    });
}

function deleteStudent(sid) {
    $.ajax({
        url: '../Student/ajaxdeletestudent',
        type: 'POST',
        data: {sid: sid},
        dataType: 'json',
        success: function (result) {
            if (result.status) {
                alert('删除成功');
                $('#' + sid).remove();
            } else {
                alert(result.errmsg);
            }

        },
        error: function (error) {
            alert('服务器故障');
        }
    })
}

function changeStudent(sid) {
    showStudent(sid, 1);


}
function changeenable() {
    $('#insertForm *').each(function () {
        $(this).attr('readonly', false);
    });
    $('#studentid').attr('readonly', true);
    var changeEnableButton = $('#changeEnableButton');
    changeEnableButton.attr('onclick', 'changeStudentInfo()');
    changeEnableButton.html('保存');
}

function findStudent(index) {
    $.ajax({
        url: '../Student/ajaxfindstudent',
        type: 'POST',
        data: {sid: $('#sid').val(), index: index},
        dataType: 'json',
        success: function (result) {
            if (result.status == true) {
                var table = $('#table tbody');
                table.html('<tr>\
                    <td>学号:</td>\
                <td>姓名:</td>\
                <td>操作:</td>\
                </tr>');
                for (i in result.info) {
                    table.append('<tr id="' + result.info[i].sid + '">\
                <td>' + result.info[i].sid + '</td>\
                <td>' + result.info[i].stuname + '</td>\
                <td><a class="btn btn-primary" onclick="showStudent(\'' + result.info[i].sid + '\')">详细</a><a class="btn btn-info" onclick="changeStudent(\'' + result.info[i].sid + '\')">修改</a><a class="btn btn-danger" onclick="deleteStudent(\'' + result.info[i].sid + '\')">删除</a> </td>\
                </tr>'
                    )
                }
            } else {
                alert(result.errmsg);
            }
        },
        error: function (status) {
            alert('服务器故障');
        }
    });
}

function changeStudentInfo() {
    if (!ifEmpty()) {
        $.ajax({
            url: '../Student/ajaxchangestudent',
            data: $('#insertForm').serialize(),
            type: 'POST',
            dataType: 'json',
            success: function (result) {
                if (result.status == true) {
                    alert('修改成功');
                } else {
                    alert('修改失败,errmsg:' + result.errmsg);
                }
            },
            error: function (error) {
                alert('服务器故障');
            }
        });
    }
}
//判断是否为空
function ifEmpty() {
    var status = false;
    $('#insertForm input[type!="radio"]').each(function () {
        if ($(this).val() == '') {
            status = true;
            return false;
        }
    });
    $('#insertForm select').each(function () {
        if ($(this).val() == '') {
            status = true;
            return false;
        }

    });
    if (!$('#insertForm input:radio[name="sex"]:checked').val()) {
        status = true;
    }
    if (status) {
        alert('请输入完整信息');
    }
    return status;
}
//获取专业，type = 1,同步，其他异步
function getSpecialty(type, stuspecialty) {
    $.ajax({
        url: '../Student/ajaxgetspecialty',
        data: {cid: $('#college option:selected').val()},
        type: 'POST',
        async: (type==1) ? false : true,
        dataType: 'json',
        success: function (result) {
            console.info(result)
            if(result.status){
                var html = '';
                for(var i in result.info){
                    html = html + '<option value="'+result.info[i].sid+'">'+result.info[i].specialtyname+'</option>'
                }
                $('#specialty').html(html);
                if (stuspecialty){
                    $('#specialty option[value = ' + result.info.stuspecialty + ']').attr('selected', true);
                }
            }else {
                $('#specialty').empty();
            }

        }
    })
}
//获取学院,type = 1,同步，其他异步
function getCollege(type) {
    $.ajax({
        url: '../Student/ajaxgetcollege',
        type: 'GET',
        dataType: 'json',
        async: (type==1) ? false : true,
        success: function (result) {
            if(result.status){
                var html = '';
                for (var i in result.info){
                    html = html + '<option value="'+i+'">'+result.info[i]+'</option>'
                }
                $('#college').html(html);
                //异步的时候才会调用，添加学生信息会调用，显示学生信息不会调用
                if(!type){
                    getSpecialty();
                }
            }else{
                console.info(result.info)
            }
        }
    });
}